﻿using BLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;

namespace WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class FangXingController : ControllerBase
    {
        public FangXingController(FangXing_BLL bll, IWebHostEnvironment Env)
        {
            Bll = bll;
            this.Env = Env;
        }

        public FangXing_BLL Bll { get; }
        public IWebHostEnvironment Env { get; }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost("TP")]
        public IActionResult TP(IFormFile file)
        {
            if (file.Length > 1024 * 1024 * 2)
            {
                return Ok(new
                {
                    data = "图片不能大于2M"
                });
            }
            var root = Env.ContentRootPath;
            var path = root + "/img/" + file.FileName;
            using (FileStream fs = System.IO.File.Create(path))
            {
                file.CopyTo(fs);
                fs.Close();
            }

            return Ok(new
            {
                data = "https://localhost:7143" + "/img/" + file.FileName

            });

        }



        /// <summary>
        /// 房型添加
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost("FangXingAdd")]
        public IActionResult FangXingAdd(FangXing_Model m)
        {
            var m1 = Bll.FangXingAdd(m);
            return Ok(new
            {
                data = m1
            });
        }
        /// <summary>
        /// 房型列表
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="FXName">房型名称</param>
        /// <param name="FXMJ">房型面积</param>
        /// <param name="Count"></param>
        /// <param name="Count2"></param>
        /// <returns></returns>
        [HttpGet("FangXingShow")]
        public IActionResult FangXingShow(int index, int size, string? FXName, string? FXMJ)
        {
            int Count = 0;
            int Count2 = 0;
            var m = Bll.FangXingShow(index, size, FXName, FXMJ, out Count, out Count2);
            return Ok(new
            {
                data = m,
                count = Count,
                count2 = Count2
            });
        }

        /// <summary>
        /// 房型删除
        /// </summary>
        /// <param name="FXId"></param>
        /// <returns></returns>
        [HttpPost("FangXingShan")]
        public IActionResult FangXingShan(int FXId)
        {
            var m = Bll.FangXingShan(FXId);
            return Ok(new
            {
                data = m
            });

        }
    }
}
